package jmine.tec.hydra.diagnostician;

import java.io.File;
import java.io.IOException;
import java.util.List;

import jmine.tec.hydra.domain.senderRegistry.AbstractSenderRegistry;
import jmine.tec.hydra.domain.senderRegistry.FileSenderRegistry;
import jmine.tec.hydra.domain.senderRegistry.MQSenderRegistry;
import jmine.tec.hydra.domain.senderRegistry.ScriptSenderRegistry;
import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.dao.BaseDAOFactory;
import bancosys.tec.rtm.diagnostician.AbstractDiagnostician;
import bancosys.tec.rtm.diagnostician.DiagnosisBean;

/**
 * Imprime os registros da subclasses {@link AbstractSenderRegistry}
 * 
 * @author takeshi
 */
public class SenderRegistryDiagnostician extends AbstractDiagnostician {

    private final BaseDAOFactory daoFactory;

    /**
     * @param daoFactory {@link BaseDAOFactory}
     */
    public SenderRegistryDiagnostician(BaseDAOFactory daoFactory) {
        this.daoFactory = daoFactory;
    }

    /**
     * {@inheritDoc}
     */
    public String getName() {
        return "Configuração de envio do Hydra";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean fillDiagnosisBean(DiagnosisBean bean) throws Exception {
        BaseDAO<AbstractSenderRegistry> dao = this.daoFactory.getGenericDAO(AbstractSenderRegistry.class);
        List<AbstractSenderRegistry> all = dao.findAll();
        for (AbstractSenderRegistry abstractSenderRegistry : all) {
            if (abstractSenderRegistry instanceof MQSenderRegistry) {
                bean.addDescription(formatMQSenderRegistry((MQSenderRegistry) abstractSenderRegistry));
            } else if (abstractSenderRegistry instanceof FileSenderRegistry) {
                bean.addDescription(formatFileSenderRegistry((FileSenderRegistry) abstractSenderRegistry));
            } else if (abstractSenderRegistry instanceof ScriptSenderRegistry) {
                bean.addDescription(formatScriptSenderRegistry((ScriptSenderRegistry) abstractSenderRegistry));
            } else {
                bean.addDescription(formatSenderRegistryPreffix(abstractSenderRegistry).append(abstractSenderRegistry.toString())
                        .toString());
            }
        }
        return true;
    }

    /**
     * Formata o {@link ScriptSenderRegistry}
     * 
     * @param scriptSenderRegistry {@link ScriptSenderRegistry}
     * @return Object
     */
    private String formatScriptSenderRegistry(ScriptSenderRegistry scriptSenderRegistry) {
        StringBuilder sb = formatSenderRegistryPreffix(scriptSenderRegistry);
        sb.append("[SCRIPT]").append(scriptSenderRegistry.getDispatchScript().getBody());
        return sb.toString();
    }

    /**
     * Formata o {@link AbstractSenderRegistry}
     * 
     * @param scriptSenderRegistry {@link AbstractSenderRegistry}
     * @return {@link StringBuilder}
     */
    private StringBuilder formatSenderRegistryPreffix(AbstractSenderRegistry scriptSenderRegistry) {
        StringBuilder sb = new StringBuilder();
        sb.append("[").append(scriptSenderRegistry.getId()).append("] ").append(scriptSenderRegistry.getMnemonico()).append(">>");
        return sb;
    }

    /**
     * Formata o sender
     * 
     * @param abstractSenderRegistry {@link FileSenderRegistry}
     * @return Object
     */
    private String formatFileSenderRegistry(FileSenderRegistry abstractSenderRegistry) {
        StringBuilder sb = formatSenderRegistryPreffix(abstractSenderRegistry);
        sb.append("\t[PATH]").append(abstractSenderRegistry.getPath()).append("\t[EXT]").append(abstractSenderRegistry.getExtension());
        File file = new File(abstractSenderRegistry.getPath());
        if (file.exists()) {
            sb.append(" exits ");
            if (file.isDirectory()) {
                sb.append(" is a directory");
                try {
                    File tmp = File.createTempFile("diag", abstractSenderRegistry.getExtension(), file);
                    sb.append(" can be written");
                    tmp.delete();
                    tmp.deleteOnExit();
                } catch (IOException e) {
                    sb.append(" cannot write: " + e.getMessage());
                }
            } else {
                sb.append(" is not a directory!!! [WARN]");
            }
        } else {
            sb.append(" does not exist!! [WARN]");
        }
        return sb.toString();
    }

    /**
     * Formata o {@link MQSenderRegistry}
     * 
     * @param abstractSenderRegistry {@link MQSenderRegistry}
     * @return Object
     */
    private String formatMQSenderRegistry(MQSenderRegistry abstractSenderRegistry) {
        StringBuilder sb = formatSenderRegistryPreffix(abstractSenderRegistry);
        sb.append("[QUEUE_CONNECTION_FACTORY]]").append(abstractSenderRegistry.getQueueConnectionFactory()).append("\t[QUEUE]")
                .append(abstractSenderRegistry.getQueue());
        return sb.toString();
    }

}
